<?php
/**
 * @package Joomla
 * @subpackage Jmonoslideshow
 * @copyright (C) 2010 - Matthieu BARBE - www.ccomca.com
 * @license GNU/GPL v2
 * 
 * Jmonoslideshow is a Joomla extension for Monoslideshow.
 *
 * Jmonoslideshow uses :
 * Monoslideshow (http://www.monoslideshow.com/)
 * Plupload (http://www.plupload.com/) by Moxicode Systems
 * json class by Michal Migurski, Matt Knapp, Brett Stimmerman
 * jquery a JavaScript Library (http://jquery.com/)
 * getid3 library (http://getid3.sourceforge.net/)
 * swfobject is an easy-to-use and standards-friendly method to embed Flash content, which utilizes one small JavaScript file (http://code.google.com/p/swfobject/)
 * Chained Selects jQuery Plugin (http://www.appelsiini.net/2010/jquery-chained-selects)
 *
 * Jmonoslideshow is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.view');

//require helper slideshow
require_once( JPATH_BASE.DS.'components'.DS.'com_jmonoslideshow'.DS.'helpers'.DS.'slideshow.php' );

/**
 * @package		Joomla
 * @subpackage	Jmonoslideshow
 */
class JmonoslideshowViewAlbums extends JView
{
	function display($tpl = null)
	{
		global $mainframe;

		$user		= &JFactory::getUser();
		$pathway	= &$mainframe->getPathway();
		$model		= &$this->getModel();

		// Get the parameters of the active menu item
		$menus	= &JSite::getMenu();
		$menu    = $menus->getActive();

		$pparams = &$mainframe->getParams();
		
		$albums		= $model->getAlbums( $pparams );
		$images		= $model->getImages( $pparams );
		
		// Album -> get start index into albums verctor && active images  per album
		$this->_init_Additional_Album_And_Image_Data( $albums, $images );
		
		$this->assignRef('albums',	$albums);
		$this->assignRef('images',	$images);
		$this->assignRef('params',		$pparams);

		parent::display($tpl);
	}
	
	/**
     * Returns a filter string for quote
     */
	function filterQuote ($string)
	{
		$string = htmlspecialchars($string, ENT_QUOTES);
		return $string = str_replace(CHR(13).CHR(10),"",$string);
	}
	
	function _init_Additional_Album_And_Image_Data(&$albums, &$images)
	{
		// for Albums:
		// a) retrieve start index into the article array for each album
		//    (note that articles have been "ordered" by categories in our SQL statement, thus 
		//    all articles of one categroy are "bundled" together one after the other)
		// b) calculate the number of active articles( #actually displayed articles -> this will be a number 
		//    between "0 < activeItems < numitems", depending on the filtering criteriums)
		 
		// values needed for generating URLs
		
			
		// LOOP over all albums
		// -> store Idx into "$albums" vector for every albumid
		for ($i=0; $i < sizeof($albums); $i++)
		{	
			$album = & $albums[$i];
			$album->item_startIdx = -1;
			$album->numitems_active = 0;
			
			$albumIdx[$album->id] = $i;
		}
		
		// LOOP over all images
		for ($i=0; $i < sizeof($images); $i++)
		{	
			$image = & $images[$i]; 		
			
			// skip uncategorized image for now
			if ($image->catid == 0)
				continue;
			
			// -> get item_startIdx && refresh numitems_active
			$albIdx = $albumIdx[$image->albumid];			
			$album = & $albums[$albIdx];
			
			if ($album->item_startIdx == -1)
				$album->item_startIdx = $i;
			$album->numitems_active += 1;
			
			
		}
	}
}
